#==============================================================================
# ■ Sprite_Character
#------------------------------------------------------------------------------
# 　角色显示用脚本。监视 Game_Character 类的实例、
# 自动变化脚本状态。
#==============================================================================

class Sprite_Character < RPG::Sprite
    #--------------------------------------------------------------------------
    # ● 定义实例变量
    #--------------------------------------------------------------------------
    attr_accessor :character                # 角色
    attr_accessor :time                # 计时器
    attr_accessor :type                # 种类
    attr_accessor :param                # 参数
    #--------------------------------------------------------------------------
    # ● 初始化对像
    #     viewport  : 查看端口
    #     character : 角色 (Game_Character)
    #--------------------------------------------------------------------------
    def initialize(viewport, character = nil)
      super(viewport)
      @character = character
      @type = 0
      update
    end
    #--------------------------------------------------------------------------
    # ● 更新画面
    #--------------------------------------------------------------------------
    def update
      super
      # 元件 ID、文件名、色相与现在的情况存在差异的情况下
      if @tile_id != @character.tile_id or
         @character_name != @character.character_name or
         @character_hue != @character.character_hue
        # 记忆元件 ID 与文件名、色相
        @tile_id = @character.tile_id
        @character_name = @character.character_name
        @character_hue = @character.character_hue
        # 元件 ID 为有效值的情况下
        if @tile_id >= 384
          self.bitmap = RPG::Cache.tile($game_map.tileset_name,
            @tile_id, @character.character_hue)
          self.src_rect.set(0, 0, 32, 32)
          self.ox = 16
          self.oy = 32
        # 元件 ID 为无效值的情况下
        else
          self.bitmap = RPG::Cache.character(@character.character_name,
            @character.character_hue)
          @cw = bitmap.width / 4
          @ch = bitmap.height / 4
          self.ox = @cw / 2
          self.oy = @ch
        end
      end
      # 设置可视状态
      self.visible = (not @character.transparent)
      # 图形是角色的情况下
      if @tile_id == 0
        # 设置传送目标的矩形
        sx = @character.pattern * @cw
        sy = (@character.direction - 2) / 2 * @ch
        self.src_rect.set(sx, sy, @cw, @ch)
      end
      # 设置脚本的坐标
      self.x = @character.screen_x
      self.y = @character.screen_y
      self.z = @character.screen_z(@ch)
      # 设置不透明度、合成方式、茂密
     # if self.opacity != @character.opacity && @character==$game_player
     #   p self.opacity,@character.opacity
     # end
      self.opacity = @character.opacity
      self.blend_type = @character.blend_type
      self.bush_depth = @character.bush_depth
      # 动画
      if @character.animation_id != 0
        animation = $data_animations[@character.animation_id]
        animation(animation, true)
        @character.animation_id = 0
      end
      self.angle=0
      if @type>0
        case @type
        when 1
          self.y = self.y + ((@time-64).abs-32)/8.0
          @time = (@time+1)%128
        when 2
          self.y = self.y + ((@time-64).abs-32)/4.0
          @time = (@time+1)%128
        end
        if @type>=3 && @type<=8
          self.ox = 32
          self.oy = 32
          self.y = self.y - 16
          v1 = @type%2 == 0 ? -1 : 1
          v2 = (@type+1)/2
          case v2
          when 2
            v3 = 6
          when 3
            v3 = 4
          when 4
            v3 = 3
          end
          v4 = 360 / v3
          self.angle=v1*v3*@time
          @time = (@time+1)%v4
          self.z=-1
        end
        if @type==9
          self.ox = @cw/2
          self.oy = @ch/2
          self.y = self.y - 16
          v1=360/param
          self.angle=param*@time
          @time = (@time+1)%v1
        end
      end
      
      #滚筒地图处理
      if $game_switches[72] && @character != $game_player
        zt=999
        x=@character.x
        @len=$game_variables[297]-$game_variables[296]
        if x-$game_player.x>@len/2
          x=x-@len
        end
        if $game_player.x-x>@len/2
          x=x+@len
        end
        x=x-$game_player.x
        if x.abs>@len/4+1
          xx=@len/2-x
          xx=xx*32+384
          self.visible=false
        else
          self.visible=true
          xx=x*32+384
        end
        pt=$game_player.real_x%128/4
        if $game_player.direction==2 || $game_player.direction==8
          if x.abs==@len/4+1
            self.visible=false
          end
        end
        if $game_player.direction==4
          if x==@len/4+1
            self.visible=false
            if pt>0
              xx+=pt*2-64
              self.visible=true
              self.z -= zt
            end
          end
          if -x==@len/4+1
            self.visible=false
          end
          if -x==@len/4
            if pt>0 
              xx+=pt*2
              self.z -= zt
            end
          end
        end
        if $game_player.direction==6
          if pt>0
            xx+=32
          end
          if -x==@len/4+1
            self.visible=false
            if pt>0
              xx+=pt*2
              self.visible=true
              self.z -= zt
            end
          end
          if x==@len/4+1
            self.visible=false
          end
          if x==@len/4
            if pt>0
              xx+=pt*2-64
              self.z -= zt
              self.visible=true
            end
          end
        end
        et=@character.real_x%128/4
        if et>0
          if @character.real_x>@character.x*128
            pt+=32-et
          end
          if @character.real_x<@character.x*128
            pt+=32-et-32
          end
        end
        self.x=xx-pt+16
      end
    end
  end
  